package beerAgent;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class BeerPanel extends JPanel {

	private BeerModel model;

	private int labelSize = 25;
	private static ImageIcon object;
	private static ImageIcon blank;
	private static ImageIcon player;

	public BeerPanel(BeerModel model) {
		this.model = model;
		initGUI();
	}

	private void initGUI() {
		this.setLayout(new GridBagLayout());
		// drawGame();
	}
	
	public void drawGame(){
		this.removeAll(); 
		int rows = model.getMapHeight();
		int cols = model.getMapWidth();
		for (int row = 0; row < rows; row ++){
			for (int col = 0; col < cols; col++){
					drawCell(row,col); 
			}
		}
		
		this.validate();
		this.repaint(); 
	}
	
	private void drawCell(int row, int col){
		GridBagConstraints con = getGridConstraints(row, col); 
		JPanel panel = new JPanel();
		char ch = model.getCell(row, col);
		if(ch == 'p') {
			panel.setBackground(Color.red);
		}
		else if(ch == 'o') {
			panel.setBackground(Color.green);
		}
		panel.setPreferredSize(new Dimension(labelSize, labelSize));
		panel.setMinimumSize(new Dimension(labelSize, labelSize));
		panel.setBorder(BorderFactory.createLineBorder(Color.black));
//		panel.setSize(new Dimension(labelSize, labelSize));
//		panel.setSize(labelSize, labelSize);
		this.add(panel,con); 
	}
	
	private GridBagConstraints getGridConstraints(int row, int col) {
		GridBagConstraints con = new GridBagConstraints(); 
		//con.insets = new Insets(1,1,1,1);
		con.gridx = col; 
		con.gridy = row;
		return con;
	}
	
	@Override 
	public Dimension getPreferredSize(){
		int width = model.getMapWidth()*labelSize; 
		int height = model.getMapHeight()*labelSize; 
		return new Dimension(width , height);
	}
	
	@Override
	public Dimension getMinimumSize(){
		return getPreferredSize();
	}
}
